import React from 'react'
import { Form, Input, Button } from 'antd'
import PropTypes from 'prop-types'
import RightsViewer from './RightViewer'
const FormItem = Form.Item

class IcsComponentsSupplierFilterForm extends React.Component {
    state = {
        query: {},
        showViewer: false
    }
    componentDidMount () {
    }
    handleReset = () => {
        this.props.form.resetFields()
        this.props.doSearch({clear: true})
    }
    handleSearch = (e) => {
        e.preventDefault()
        this.props.form.validateFields((err, values) => {
            this.props.doSearch(values)
        })
    }
    createRights = () => {
        this.setState({showViewer: true})
    }
    doViewer = () => {
        this.setState({showViewer: false})
    }
    handleSearch = (e) => {
        e.preventDefault()
        this.props.form.validateFields((err, values) => {
            let newValue = {}
            for (let k in values) {
                if (typeof values[k] === 'string') {
                    newValue[k] = values[k].replace(/^\s+|\s+$/g,"")
                } else {
                    newValue[k] = values[k]
                }
            }
            this.props.doSearch(newValue)
        })
    }
    render () {
        const { getFieldDecorator } = this.props.form
        return <Form
            layout="inline"
            className="supplier-filter"
            onSubmit={this.handleSearch}
        >
            <FormItem
                label="权限码"
                labelCol={{ span: 10 }}
                wrapperCol={{ span: 14 }}>
                {getFieldDecorator('pri_code', {
                })(
                    <Input placeholder="请输入权限码" />
                )}
            </FormItem>
            <FormItem
                label="权限名称"
                labelCol={{ span: 10 }}
                wrapperCol={{ span: 14 }}>
                {getFieldDecorator('pri_name', {
                })(
                    <Input placeholder="请输入权限名称" />
                )}
            </FormItem>
            <FormItem className="filter-select">
                <Button type="primary" htmlType="submit" style={{marginRight: 10}} >查询</Button>
            </FormItem>
            <FormItem className="filter-select">
                <a onClick={this.handleReset}>清除条件</a>
            </FormItem>
            <Button type="primary" style={{marginRight: 10}} onClick={this.createRights}>新建权限</Button>
            <RightsViewer showViewer={this.state.showViewer} doSearch={this.props.doSearch} isEditing={false} doViewer={this.doViewer}></RightsViewer>
        </Form>
    }
}

IcsComponentsSupplierFilterForm.propTypes = {
    getFieldDecorator: PropTypes.func,
    doSearch: PropTypes.func,
    form: PropTypes.object
}

const IcsComponentsSupplierFilter = Form.create()(IcsComponentsSupplierFilterForm)

export default IcsComponentsSupplierFilter
